package com.klcw.distributedCommunication.serialization.clone;

import java.io.*;

/**
 * @ClassName Student
 * @Description TODO
 * @Author dukang
 * @Date 2019/1/30 17:04
 * @Version 1.0
 **/
public class Student implements Serializable {
    private static final long serialVersionUID = 6328870316363641069L;

    private String name;

    private int age;

    private Teacher teacher;

    // 通过序列化实现深拷贝
    public Object deepClone() throws IOException, ClassNotFoundException {
        // 通过字节流写入数据
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);

        // 反序列化数据  toByteArray 从输出流复制一个流大小和缓存区有效内容的字节数组
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);

        return ois.readObject();

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", teacher=" + teacher +
                '}';
    }
}
